<template>
  <Card dis-hover :bordered="false">
    <Form inline :label-width="0" @submit.native.prevent>
      <Row>
        <Col span="22">
          <FormItem>
            <Input
              placeholder="请输入医生名称"
              v-model="params.keywords"
              @on-clear="searchData"
              @on-enter="searchData"
              clearable
            />
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="请选择项目"
              v-model="params.project_id"
              :filterable="projectList.length >= 10"
              @on-open-change="showAll"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in projectList"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="医生类型"
              v-model="params.type"
              :filterable="typeList.length >= 10"
              @on-open-change="showAll"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in typeList"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="请选择状态"
              v-model="params.status"
              @on-clear="searchData"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in statusList"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
        </Col>
        <Col span="2" style="text-align: right">
          <Button
            type="primary"
            @click="handleAdd"
            class="ivu-ml-8"
            v-hasC="{
              name: 'v1-buss-doctors-update-doctors',
              flag: 'v1/buss/doctors/update-doctors',
            }"
            >新增</Button
          >
        </Col>
      </Row>
    </Form>
    <Table
      :columns="columns"
      :data="list"
      :loading="loading"
      @on-sort-change="sortChange"
      :max-height="tableHeigth"
    >
      <template #status="{ row }">
        <Badge
          status="success"
          :text="row.status_name"
          v-if="row.status === '1'"
        />
        <Badge status="error" :text="row.status_name" v-else />
      </template>
      <template #action="{ row }">
        <a
          @click="handleEdit(row)"
          v-hasC="{
            name: 'v1-buss-doctors-update-doctors2',
            flag: 'v1/buss/doctors/update-doctors',
          }"
          >编辑</a
        >
        <Divider
          type="vertical"
          v-hasC="{
            name: 'v1-buss-doctors-update-doctors2',
            flag: 'v1/buss/doctors/update-doctors',
          }"
        />
        <a
          @click="handleDel(row.id)"
          class="del"
          v-hasC="{
            name: 'v1-buss-doctors-delete-doctors',
            flag: 'v1/buss/doctors/delete-doctors',
          }"
          >删除</a
        >
      </template>
    </Table>
    <Page
      class="ivu-mt-16"
      :total="total"
      :modelValue="params.current"
      :page-size="params.size"
      :page-size-opts="pageOpts"
      show-total
      show-sizer
      @on-change="changePage"
      @on-page-size-change="changepageSize"
    />
    <Modal
      :mask-closable="false"
      v-model="addModal"
      :title="edit ? '新增' : '编辑'"
      :footer-hide="true"
      :draggable="true"
    >
      <Form ref="create" :model="doctorInfo" :rules="rules" :label-width="80">
        <FormItem label="医生" prop="name" required>
          <Input
            v-model="doctorInfo.name"
            placeholder="医生姓名"
            :maxlength="30"
          />
        </FormItem>
        <FormItem label="推广星级" prop="pop_stars">
          <Select
            v-model="doctorInfo.pop_stars"
            placeholder="推广星级"
            :filterable="popStarsAddList.length >= 10"
            @on-open-change="showAll"
            clearable
          >
            <Option
              v-for="item in popStarsAddList"
              :value="+item.value"
              :key="item.value"
              >{{ item.label }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="医院" prop="hospital_name">
          <Input
            v-model="doctorInfo.hospital_name"
            placeholder="医院名称"
            :maxlength="30"
          />
        </FormItem>
        <FormItem label="医生类型" prop="type" required>
          <Select
            v-model="doctorInfo.type"
            placeholder="医生类型"
            :filterable="typeAddList.length >= 10"
            @on-open-change="showAll"
            clearable
          >
            <Option
              v-for="item in typeAddList"
              :value="+item.value"
              :key="item.value"
              >{{ item.label }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="项目" prop="project_id" required>
          <Select
            v-model="doctorInfo.project_id"
            placeholder="项目"
            :filterable="projectAddList.length >= 10"
            @on-open-change="showAll"
            clearable
          >
            <Option
              v-for="item in projectAddList"
              :value="+item.value"
              :key="item.value"
              >{{ item.label }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="状态" prop="status">
          <Switch v-model="doctorInfo.status" />
        </FormItem>
      </Form>
      <div slot="footer" style="text-align: right">
        <Button type="default" @click="addModal = false">取消</Button>
        <Button
          type="primary"
          @click="ok"
          :loading="btnLoading"
          style="margin-left: 10px"
          >确定</Button
        >
      </div>
    </Modal>
  </Card>
</template>
<script>
import {
  doctorsList,
  createDoctors,
  deleteDoctors,
  doctorsCondition,
} from "@api/doctor.js";
export default {
  name: "doctor",
  data() {
    return {
      tableHeigth: document.body.clientHeight - 280,
      addModal: false,
      edit: true,
      params: {
        keywords: "",
        project_id: "",
        type: "",
        status: "",
        current: 1,
        size: 10,
        order_column: "",
        order_sort: "",
      },
      projectList: [],
      typeList: [],
      statusList: [],
      popStarsAddList: [],
      projectAddList: [],
      typeAddList: [],
      total: 0,
      pageOpts: [10, 20, 30, 40],
      columns: [
        {
          key: "id",
          title: "ID",
          width: 80,
        },
        {
          key: "name",
          title: "医生",
          width: 120,
        },
        {
          key: "account_num",
          title: "账号数",
          width: 100,
          sortable: "custom",
        },
        {
          key: "pop_stars_name",
          title: "推广星级",
          width: 100,
        },
        {
          key: "hospital_name",
          title: "医院",
          minWidth: 140,
        },
        {
          key: "type_name",
          title: "医生类型",
          width: 120,
        },
        {
          key: "project_name",
          title: "项目",
          width: 120,
        },
        {
          slot: "status",
          title: "状态",
          width: 100,
        },
        {
          key: "operation_name",
          title: "操作人",
          width: 100,
        },
        {
          slot: "action",
          title: "操作",
          width: 130,
          fixed: "right",
        },
      ],
      list: [],
      loading: false,
      btnLoading: false,
      doctorInfo: {
        id: "",
        name: "",
        pop_stars: "",
        hospital_name: "",
        type: "",
        project_id: "",
        status: true,
      },
      rules: {
        project_id: [{ required: true, message: "请选择项目" }],
        name: [{ required: true, message: "请输入医生名称" }],
        type: [{ required: true, message: "请选择医生类型" }],
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    showAll(e) {
      if (e) {
        let downList = document.querySelectorAll(
          ".ivu-select-dropdown-list .ivu-select-item"
        );
        downList.forEach((item) => {
          item.style.display = "block";
        });
      }
    },
    //切换page
    changePage(index) {
      this.params.current = index;
      this.getData();
    },
    changepageSize(val) {
      this.params.size = val;
      this.getData();
    },
    sortChange(e) {
      this.params.order_column = e.key.replace(/_str$/, "");
      this.params.order_sort = e.order;
      this.getData();
    },
    handleDel(index) {
      this.$Modal.confirm({
        title: "删除提示",
        content: "是否确认删除此条记录，删除后不可恢复",
        onOk: () => {
          deleteDoctors({ id: index }).then(() => {
            this.$Message.success("删除成功");
            this.getData();
          });
        },
      });
    },
    handleAdd() {
      doctorsCondition().then((res) => {
        this.popStarsAddList = res.condition.pop_stars_arr;
        this.projectAddList = res.condition.project_arr;
        this.typeAddList = res.condition.type_arr;
        this.doctorInfo = {
          id: "",
          name: "",
          pop_stars: "",
          hospital_name: "",
          type: "",
          project_id: "",
          status: true,
        };
      });

      this.addModal = true;
      this.edit = true;
      this.$refs.create.resetFields();
    },
    handleEdit(info) {
      this.addModal = true;
      this.edit = false;
      doctorsCondition().then((res) => {
        this.popStarsAddList = res.condition.pop_stars_arr;
        this.projectAddList = res.condition.project_arr;
        this.typeAddList = res.condition.type_arr;
        this.doctorInfo = {
          id: info.id,
          name: info.name,
          pop_stars: +info.pop_stars,
          hospital_name: info.hospital_name,
          type: +info.type,
          project_id: +info.project_id,
          status: info.status == "1" ? true : false,
        };
      });
    },
    getData() {
      this.loading = true;
      const params = {
        keywords: this.params.keywords,
        project_id: this.params.project_id,
        type: this.params.type,
        status: this.params.status,
        order_column: this.params.order_column,
        order_sort: this.params.order_sort,
        pageSize: this.params.size,
        page: this.params.current,
      };
      doctorsList(params)
        .then((res) => {
          this.projectList = res.condition.project_arr;
          this.statusList = res.condition.status_arr;
          this.typeList = res.condition.type_arr;
          this.list = res.list;
          this.total = res.total;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    searchData() {
      this.params.current = 1;
      this.getData();
    },
    addDoctor() {
      this.btnLoading = true;
      const params = {
        name: this.doctorInfo.name,
        pop_stars: this.doctorInfo.pop_stars,
        hospital_name: this.doctorInfo.hospital_name,
        type: this.doctorInfo.type,
        project_id: this.doctorInfo.project_id,
        status: this.doctorInfo.status ? 1 : 0,
      };
      if (this.edit) {
        createDoctors(params)
          .then((res) => {
            this.$Message.success("录入成功");
            this.addModal = false;
            this.btnLoading = false;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      } else {
        createDoctors(Object.assign({}, params, { id: this.doctorInfo.id }))
          .then((res) => {
            this.$Message.success("编辑成功");
            this.addModal = false;
            this.btnLoading = false;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      }
    },
    ok() {
      this.$refs.create.validate((valid) => {
        if (valid) {
          this.addDoctor();
        } else {
          this.addModal = true;
          return;
        }
      });
    },
  },
};
</script>
